package com.digitalchina.lxnsy.activity.management.message;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.activity.standard.PlanFarmingRecordDetailsActivity;
import com.digitalchina.lxnsy.base.BaseFragment;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.MsgListVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.OkHttpUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.MsgListHolder;
import com.digitalchina.lxnsy.xml.AdminXML;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.lzy.okhttputils.model.HttpParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity.management
 * 创建人：郑皓
 * 创建时间：2019/4/26 16:39
 * 修改备注：
 * ================================================
 */
public class MsgListFragment extends BaseFragment implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private String type = "";

    private List<MsgListVO> voList = new ArrayList<>();

    @Override
    public View setRootView() {
        return inflate(R.layout.fragment_msg_list);
    }

    @Override
    public void initMember() {

        type = getArguments().getString("type");

        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        recyclerView.addItemDecoration(Utils.setDividerDecoration(getActivity(), 1.0f, 0.0f, 0.0f));

        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getActivity()) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new MsgListHolder(parent);
            }
        });

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);

        adapter.setMore(R.layout.view_more, this);

        adapter.setNoMore(R.layout.view_nomore);

        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                MsgListVO vo = (MsgListVO) adapter.getAllData().get(position);
                vo.setIslook("1");
                adapter.notifyDataSetChanged();
                goDetail(vo);

            }
        });
    }

    /**
     * messageType
     * 消息类型
     * 1 农产品采购
     * 2 种子采购
     * 3 农资出入库
     * 4 出库销售
     * 5 育苗
     * 6 定植
     * 7 浇水施肥
     * 8 中耕除草
     * 9 农药使用
     * 10 采收
     * 11 农事操作
     */
    private void goDetail(MsgListVO vo) {
        switch (vo.getMessageType()){
            case "1":
                GotoUtil.gotoActivity(getActivity(), DetailFarmProductActivity.class, "MsgListVO", vo);
                break;
            case "2":
                GotoUtil.gotoActivity(getActivity(), DetailSeedActivity.class, "MsgListVO", vo);
                break;
            case "3":
                GotoUtil.gotoActivity(getActivity(), DetailOutInActivity.class, "MsgListVO", vo);
                break;
            case "4":
                GotoUtil.gotoActivity(getActivity(), DetailProductOutActivity.class, "MsgListVO", vo);
                break;
            case "5":
                GotoUtil.gotoActivity(getActivity(), DetailGrowSeedActivity.class, "MsgListVO", vo);
                break;
            case "6":
                GotoUtil.gotoActivity(getActivity(), DetailPlantingActivity.class, "MsgListVO", vo);
                break;
            case "7":
                GotoUtil.gotoActivity(getActivity(), DetailWateringActivity.class, "MsgListVO", vo);
                break;
            case "8":
                GotoUtil.gotoActivity(getActivity(), DetailWeedingActivity.class, "MsgListVO", vo);
                break;
            case "9":
                GotoUtil.gotoActivity(getActivity(), DetailPesticideActivity.class, "MsgListVO", vo);
                break;
            case "10":
                GotoUtil.gotoActivity(getActivity(), DetailHarvestActivity.class, "MsgListVO", vo);
                break;
            case "11":
                GotoUtil.gotoActivity(getActivity(), PlanFarmingRecordDetailsActivity.class, "MsgListVO", vo);
                break;
        }
    }


    @Override
    public void setHandler() {

        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                switch (message.what) {
                    case MSG_S_0: // 获取列表
                        callBack(message.obj);
                        break;
                    case MSG_F_0:
                        showToast(message.obj.toString());
                        break;
                    case MSG_S_1:

                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void callBack(Object object) {


        voList = FastJsonUtil.getListBean(object.toString(), "messageVOList", MsgListVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(voList);

        if (voList != null) {
            if (voList.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        getMsgList();
    }

    @Override
    public void onLoadMore() {
        if (voList != null && voList.size() >= limit) {
            page++;
            getMsgList();
        }
    }

    public void getMsgList(){

        /**
         * enterpriseId	企业ID
         * type	类型（0 全部 /1 已经查看 /2 待查看）
         * userAccId	用户ID
         * pageNum	页数（第几页）
         * pageSize	每页数量
         */
        HttpParams params = new HttpParams();
        params.put("enterpriseId", AdminXML.getEnterpriseId(getActivity()));
        params.put("resNo", AdminXML.getResNo(getActivity()));
        params.put("userAccId", AdminXML.getUserAccId(getActivity()));
        params.put("type",  type);
        params.put("pageNum",  String.valueOf(page));
        params.put("pageSize", String.valueOf(limit));

        OkHttpUtil.post(getActivity(), URL.URL_GET_MESSAGE_LIST, "", params, mHandler,
                MSG_S_0, MSG_F_0);
    }
}
